<?php

class Application_Model_ProductBuyBidMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_ProductBuyBid');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_ProductBuyBid $obj)
    {
        $data = array(
        	'product_buy_id' => $obj->getProduct_buy_id(),
            'member_id' => $obj->getMember_id(),
        	'price'   => $obj->getPrice(),
        	'offer_type'   => $obj->getOffer_type(),
        	'status'   => $obj->getStatus(),
        	'checked'   => $obj->getChecked(),
        	'creation_time' => $obj->getCreation_time()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_ProductBuyBid $obj)
    {
    	$db = $this->getDbTable();
    	
         $data = array(
         	'product_buy_id' => $obj->getProduct_buy_id(),
            'member_id' => $obj->getMember_id(),
         	'price'   => $obj->getPrice(),
         	'offer_type'   => $obj->getOffer_type(),
        	'status'   => $obj->getStatus(),
        	'checked'   => $obj->getChecked(),
        	'creation_time' => $obj->getCreation_time()
        );
        $db->update($data,array('product_buy_bid_id = ?' => $obj->getProduct_buy_bid_id()));
    }
    
	public function deleteProductBuyBid($product_buy_bid_id)
    {
        $this->getDbTable()->delete(array('product_buy_bid_id = ?' => $product_buy_bid_id));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_ProductBuyBid();
			$entry->setProduct_buy_bid_id($row->product_buy_bid_id)
				->setProduct_buy_id($row->product_buy_id)
				->setMember_id($row->member_id)
				->setPrice($row->price)
				->setOffer_type($row->offer_type)
				->setStatus($row->status)
				->setChecked($row->checked)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findAll()
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('product_buy_bid')
        	->order('creation_time DESC');
    	return $this->findByResult($select);
    }
    
	public function findByProduct_buy_bid_id($product_buy_bid_id)
    {
    	if($product_buy_bid_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_bid')
	             ->where('product_buy_bid_id = ?', $product_buy_bid_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByProduct_buy_id($product_buy_id)
    {
    	if($product_buy_id != null){
	    	$db = $this->getDbTable();
	    	
    		$select = $db->select()
			->from(array('a'=>'product_buy_bid'), array('product_buy_bid_id', 'product_buy_id', 'username' => 'b.username', 
				'price', 'offer_type', 'status', 'checked', 'creation_time' => 'a.creation_time'))
			->join(array('b'=>'member'),'b.member_id = a.member_id')
			->where('product_buy_id = ?', $product_buy_id);
	    	
	    	$select->setIntegrityCheck(false); 
    	
	  		$resultSet = $db->fetchAll($select);
	        $entries   = array();
			foreach ($resultSet as $row) {
				$entry = new Application_Model_ProductBuyBid();
				$entry->setProduct_buy_bid_id($row->product_buy_bid_id)
				->setProduct_buy_id($row->product_buy_id)
				->setMember_id($row->member_id)
				->setUsername($row->username)
				->setPrice($row->price)
				->setOffer_type($row->offer_type)
				->setStatus($row->status)
				->setCreation_time($row->creation_time);
				$entries[] = $entry;
			}
	    	
	    	return $entries;
    	}
    }
    
	public function findByMemberId($member_id)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_bid')
	             ->where('member_id = ?', $member_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByPbIdMemberId($product_buy_id, $member_id)
    {
    	if($product_buy_id != null && $member_id != null){
	    	$db = $this->getDbTable();
	    	
    		$select = $db->select()->from('product_buy_bid')
	        	->where('product_buy_id = ?', $product_buy_id)
	            ->where('member_id = ?', $member_id)
	            ->where('creation_time = (select max(creation_time) from product_buy_bid where product_buy_id = "' . $product_buy_id . '" and member_id = "' . $member_id . '")');

	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberIdOfferType($member_id , $type)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_bid')
	             ->where('member_id = ?', $member_id)
	             ->where('offer_type = ?', $type)
	             ->where('checked = ?', 'N');
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findNoticeByMemberIdOfferType($member_id , $type)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	    	
    		$select = $db->select()
			->from(array('a'=>'product_buy_bid'), array('product_buy_bid_id', 'product_buy_id', 'a.member_id', 'price',
				 'offer_type', 'status', 'checked',  'creation_time' => 'a.creation_time'))
			->join(array('b'=>'product_buy'),'b.product_buy_id = a.product_buy_id')
			->where('b.member_id = ?', $member_id)
			->where('offer_type = ?', $type)
			->where('checked = ?', 'N');
	    	
	    	$select->setIntegrityCheck(false); 
    	
	  		$resultSet = $db->fetchAll($select);
	        $entries   = array();
			foreach ($resultSet as $row) {
				$entry = new Application_Model_ProductBuyBid();
				$entry->setProduct_buy_bid_id($row->product_buy_bid_id)
				->setProduct_buy_id($row->product_buy_id)
				->setMember_id($row->member_id)
				->setPrice($row->price)
				->setOffer_type($row->offer_type)
				->setStatus($row->status)
				->setCreation_time($row->creation_time);
				$entries[] = $entry;
			}
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findNoticeByPbIdOfferType($pbId , $type)
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('product_buy_bid')
             ->where('product_buy_id = ?', $pbId)
			 ->where('offer_type = ?', $type)
			 ->where('checked = ?', 'N');
    	
    	return $this->findByResult($select);
    }
    
	public function findByMemberIdPbId($member_id, $pbid)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_bid')
	             ->where('member_id = ?', $member_id)
	             ->where('product_buy_id = ?', $pbid);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberIdPbIdCreationTime($member_id, $pbid, $creation_time)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_bid')
	             ->where('member_id = ?', $member_id)
	             ->where('product_buy_id = ?', $pbid)
	             ->where('creation_time = ?' , $creation_time);
	    	
	    	return $this->findByResult($select);
    	}
    }
}

